# Mario Fireball - Interact with article after it has been used

# To understand the code better, please read these guides!!
# Code's original article wizardry guide: https://docs.google.com/document/d/1lrd140BIUFApPFadD0yflsBuu8kS9Zm7WUULvdsWEAo/
# My on the fly article floating point changing guide: https://docs.google.com/document/d/1HkvrNNnZ-QnIp53EJlvBilzunm7lgHBtHCWAAKV9FKw/
# Also I wrote this for changing the bone an article is attached to, specifically because Mario's fireball can't be easily changed so I wanted to detail the hacky (but successful) way I use (which will also work for any other article that has this issue as far as I know, eliminating the need for a code): https://docs.google.com/document/d/11V88VjX-XZKshtSEVHA873e7mN6EY34uHAEajGouBHU/edit#heading=h.t68pre5kww5b

# B: Mario will drop a fireball if not in radius of another one. This fireball will remain on the field waiting for Mario to perform an action on it. If he is in radius of another a fireball, instead he will perform a light throw down animation and "set off" the fireball to explode.
# Note: Mario has memory for five fireball instances. That means five can be generated at a time. If a sixth is generated, it deletes the first. You have no control over which instance gets created at a time, but once it has been created you can use code to determine which article you are wanting to interact with.


####### Specials - Action 112 #######

# Not much is actually changed here. Based on whether RA-Bit[5] is set, it will either execute subactions 1CE and 1CF (regular Mario neutral B) or 39 and 3A (subactions that will use "LightThrowLw" and "AirLightThrowLw".
# RA-Bit[5] will be set if Mario is in radius of a fireball article (determined by Sub Routine 1FC64, which will be gone over next).

#######

####### Sub Routine 1FC64 #######

# The code in this subaction will set RA-Bit[5] if Mario is in radius of a fireball article, as well as set a value in the fireball article to let it know it is being interacted with.

# Step 1: Create x and y range
# This is how close Mario can be to the fireball (x and y coordinate) to be considered within radius
# The code currently uses an upper and lower bounds of 6 for both x and y.
# For example, if the fireball sits at an x location of 6, Mario will be within radius from x locations 0 to C
Basic Variable Set: RA-Basic[22] = 6
Basic Variable Set: RA-Basic[23] = 6

# This creates the upper and lower bounds variables for the x coordinate to later be compared to
# RA-Basic[24] has the lower bounds while RA-Basic[25] has the upper bounds
# IC-Basic[3] is a function that returns Mario's current x coordinate location
Basic Variable Set: RA-Basic[24] = IC-Basic[3]
Basic Variable Set: RA-Basic[25] = IC-Basic[3]
Basic Variable Subtract: RA-Basic[24] -= RA-Basic[22]
Basic Variable Add: RA-Basic[25] +- RA-Basic[22]

# This creates the upper and lower bounds variables for the y coordinate to later be compared to
# RA-Basic[26] has the lower bounds while RA-Basic[27] has the upper bounds
# IC-Basic[4] is a function that returns Mario's current y coordinate location
Basic Variable Set: RA-Basic[26] = IC-Basic[4]
Basic Variable Set: RA-Basic[27] = IC-Basic[4]
Basic Variable Subtract: RA-Basic[26] -= RA-Basic[23]
Basic Variable Add: RA-Basic[27] +- RA-Basic[23]

# Step 2: Compare to article's current location to see if within radius
# You will notice that there are five blocks of code that are nearly identical besides variable numbers that look like this:
If Compare: RA-Basic[29833] == 1.0
    And Not Bit is Set: RA-Bit[5]
    If Compare: RA-Basic[30016] >= RA-Basic[24]
        And Comparison Compare: RA-Basic[30016] <= RA-Basic[25]
        And Comparison Compare: RA-Basic[30017] >= RA-Basic[26]
        And Comparison Compare: RA-Basic[30017] <= RA-Basic[27]
        Basic Variable Set: RA-Basic[29834] = 1
        Bit Variable Set: RA-Bit[5] = true
    End If:
End If:

# This step has multiple parts that all come together due to the fact that Mario needs to interact with a specific article instance, but has no personal control over which article instance gets created/which one is which
# Remember, there are five possible fireball instances, so five checks will need to be done on each fireball.
# This step will make more sense later on when we go over the article code.

# This checks to see if the article is "active" -- meaning it is currently spawned in game
# The article sets this variable itself upon creation (RA-Basic[1] in the article instance)
# RA-Basic[29833] in Mario's moveset accesses the first article instance's RA-Basic[1]
If Compare: RA-Basic[29833] == 1.0

# This check for if RA-Bit[5] is set is just to make sure that only one article at a time can be triggered.
And Not Bit is Set: RA-Bit[5]

# Next, the current x and y position of the article is checked against Mario's current x and y positions (upper and lower bounds) to see if Mario is within Radius
# is article's x greater than or equal to Mario's lower bound x and less than or equal to Mario's upper bound x?
If Compare: RA-Basic[30016] >= RA-Basic[24]
And Comparison Compare: RA-Basic[30016] <= RA-Basic[25]
And Comparison Compare: RA-Basic[30017] >= RA-Basic[26]
And Comparison Compare: RA-Basic[30017] <= RA-Basic[27]

# If Mario is within radius of that article instance, we let the article know by setting it's RA-Basic[2] variable to 1. 
# The article can then do with this info what it wishes.
Basic Variable Set: RA-Basic[29834] = 1

# We then set RA-Bit[5] to true, to let the special action 112 know as well as to prevent any other articles from triggering:
Bit Variable Set: RA-Bit[5] = true

# Note: Each of these five checks looks at each article instance. Without all 5, some article instances would trigger while others would not, and you once again do not have control over which article instance is spawned, so make sure that all 5 are checked against like how it is done here.

#######

####### Article 1 (fireball) Action 0 #######

# This action is triggered upon fireball creation, so I simply set a bunch of variables to default values that will be used within the article.
# I find this to be good practice as article code can get messy if doing a lot, especially since not all variables are usable

# These are just some flags that I use at times:
Bit Variable Clear: LA-Bit[0] = false
Bit Variable Clear: LA-Bit[1] = false
Bit Variable Clear: LA-Bit[2] = false

# Setting RA-Basic[1] to 1 tells Mario that the article is active.
# The article will automatically reset RA-Basic[1] to 0 upon despawning due to how RA variables work. 
Basic Variable Set: RA-Basic[1] = 1

# RA-Basic[2] can be set by 1 by Mario in Sub Routine 1FC64 to tell it that Mario has triggered it
# RA-Basic[3] is automatically set to the value of RA-Basic[2] each frame. RA-Basic[3] is then used within the article to check for triggering -- this will be explained later.
Basic Variable Set: RA-Basic[2] = 0
Basic Variable Set: RA-Basic[3] = 0

# LA-Basic[31] and LA-Basic[32] default to the current x and y coordinate location of the article instance
# Just like in Mario's moveset, the functions IC-Basic[3] and IC-Basic[4] work the same and return the current x and y coordinate for the article respectively
Basic Variable Set: LA-Basic[31] = IC-Basic[3]
Basic Variable Set: LA-Basic[32] = IC-Basic[4]

# From Mario's original code, calls a subroutine that will activate every frame of the article's existence
Concurrent Infinite Loop: 0x4, 2x1F9D4,

#######

######## Concurrent Infinite Loop 1F9D4 (the one inside Action 0)

# This loop is ran every frame of the article's existence on a separate thread, meaning any other code is running in parallel
# The fireball used to use this loop to constantly check if fireball is touching a floor, wall or ceiling, and to bounce if so

# The first part of the code is leftover from Mario -- it just checks to see how much time is currently left for the article's existence (set via article parameter x14 "Duration (frames), and upon hitting 0 will terminate the instance of the article
# LA-Basic[4] is set to the Duration parameter upon fireball creation
Basic Variable Decrement: LA-Basic[4]--
If Compare: LA-Basic[4] <= 0.0
    Terminate Instance:
End If:

# The next part checks if the article has been triggered.
If Not Bit is Set: LA-Bit[3]
    Basic Variable Set: RA-Basic[3] = RA-Basic[2]
    If Compare: RA-Basic[3] == 1.0
        Basic Variable Set: LA-Basic[4] = 64
        Bit Variable Set: LA-Bit[3] = true
    End If:
End If:

# LA-Bit[3] is just used to ensure that after the article has been triggered and the code in this block has been activated once, do not let it activate again (otherwise the action of the article triggering would happen over and over again)
If Not Bit is Set: LA-Bit[3]
...
Bit Variable Set: LA-Bit[3] = true

# The next line sets RA-Basic[3] to RA-Basic[2], which will happen every frame. This is essential for any variable you set in the article from Mario's moveset. This ensures that any variable checked for in the article is set by the article itself and not from Mario -- whlie Mario set RA-Basic[2], the article itself sets RA-Basic[3]. This will prevent any article instance confliction bugs that may occur within your code. While it isn't 100% necessary in this case, this technique should always be used for the many other cases where you do not know which instance is being created or what article it even is, but you want it to do something.
# If you look at my previous release (Mario Article Wizardy Playground) you will see that this technique is essential for keeping track of article instances
Basic Variable Set: RA-Basic[3] = RA-Basic[2]

# It then checks if RA-Basic[3] is 1, which it will be upon RA-Basic[2] being set to 1, which happens within Sub Routine 1FC64 if Mario is within raidus of the fireball instance.
# If this evaluates to true, the article has been triggered.
If Compare: RA-Basic[3] == 1.0

# Next, LA-Basic[4] is being set to a hex value of 64 (which converts to 100 in decimal).
# Remember LA-Basic[4] is the Duration (frames) of how long the fireball lasts, so I am resetting whatever it was to 100 frames.
Basic Variable Set: LA-Basic[4] = 64

#######

####### Article 1 subactions #######

# Main (goto subroutine)
Set Loop: Infinite
    If Compare: RA-Basic[3] == 1.0
        Offensive Collision: ID=0, Dmg=10, Angle=5F, WDSK=35, KBG=7F, BKB=30, Bone=0, Size=10.0, Z Offset=0.0, Y Offset=0.0, X Offset=0.0, Hitlag Mult=0.0, SDI Mult=1.0, Shield Dmg=86, Trip Rate=0.0, Flags=10030005
    End If:
    Synchronous Timer: Frames=1.0
Execute Loop:

# Subroutine contains an infinite loop. 
# It is constantly checking whether article has been triggered yet or not.
Set Loop: Infinite
    If Compare: RA-Basic[3] == 1.0

# If it has not been triggered, it does nothing
# If it has been triggered, it creates an offensive collision for the fireball explosion that occurs upon the article being triggered.
Offensive Collision: ID=0, Dmg=10, Angle=5F, WDSK=35, KBG=7F, BKB=30, Bone=0, Size=10.0, Z Offset=0.0, Y Offset=0.0, X Offset=0.0, Hitlag Mult=0.0, SDI Mult=1.0, Shield Dmg=86, Trip Rate=0.0, Flags=10030005

# GFX (goto subroutine)
# Behaves the same way as Main -- constantly loops checking whether article has been triggered or not.

# If not triggered (RA-Basic[3] is 0, it creates the little initial flame graphic effect):
# LA-Bit[1] just ensures this graphic effect is only created once, since it is an infinite graphic effect
If Compare: RA-Basic[3] == 0.0
    If Not Bit is Set: LA-Bit[1]
        Graphic Effect (Attached): File #=0, Graphic=60, Bone=0, Z Offset=0.0, Y Offset=0.0, X Offset=0.0, Z Rotate=0.0, Y Rotate=0.0, X Rotate=0.0, Size=1.0, Terminate With Animation=true
        Bit Variable Set: LA-Bit[1] = true
    End If:

# If article is triggered (RA-Basic[3] is set to 1), terminates the little initial flame graphic and creates exposion graphic
Else If Comparison Compare: RA-Basic[3] == 1.0
    Terminate Graphic Effect: 0x60, false, false,
    If Not Bit is Set: LA-Bit[2]
        Graphic Effect (Attached): File #=0, Graphic=73, Bone=0, Z Offset=0.0, Y Offset=0.0, X Offset=0.0, Z Rotate=0.0, Y Rotate=0.0, X Rotate=0.0, Size=2.0, Terminate With Animation=true
        Bit Variable Set: LA-Bit[2] = true
    End If:

#######

# Additional Notes:
# Mario's x and y locations are based on his TopN bone's position (which is down by his feet)
# The fireball article's x and y locations are based on the TopN, so center of the fireball. Notice that it never hits the ground -- this is intended by the game, so if on the ground it will have a y of 2 more than a character would. Not much of an issue, just plan your coordinate bounds accordingly!
# Mario's fireball articles will always immediately terminate upon a hitbox connecting...hardcoded by the game. Figured I'd answer this now since people have been asking. There is a code that Brawl Minus uses to remove this behavior from Mewtwo's shadowball...if only a variation could be made for Mario's fireball...